using Cysharp.Threading.Tasks;
using Framework.Helper;
using Framework.UI;
using Game.Model.ServerList;
using Game.System;
using Logic.Helper;
using Shared.UI;
using UnityEngine;
using UnityEngine.UI;

namespace Game.Logic.Auth.Login
{
    public class LoginView : UIView
    {
        public InputField iptAccount;
        public Button btnChangeAccount;
        
        public InputField iptServer;
        public Button btnChangeServer;

        public Button btnEnterGame;
        
        private Login login;
        
        private void Start()
        {
            login = controller as Login;
            
            btnEnterGame.AddListener(OnEnterGame);
            btnChangeServer.AddListener(OnServerList);
        }

        public override async UniTask RefreshUI()
        {
            await UniTask.CompletedTask;

            ServerInfoDto serverInfo = AccountSystem.Instance.GetCurrentServer();
            iptServer.text = serverInfo.name + $"（{serverInfo.ip}:{serverInfo.port}）";
        }

        private async UniTask OnEnterGame()
        {
            if (string.IsNullOrEmpty(iptAccount.text))
            {
                Debug.LogError("请输入账号");
                return;
            }
            
            await UniTask.CompletedTask;

            await SwitchSceneHelper.Login2Lobby();
            // await login.Active<CreateRole.CreateRole>(UIId.CreateRole);
        }

        private async UniTask OnServerList()
        {
            await login.Deactive(login.Id);
            await UIManager.Instance.Active<ServerList.ServerList>(UIId.ServerList);
        }
    }
}